<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['basic:categories:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >上传</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="categoriesList" @selection-change="handleSelectionChange">
      <el-table-column label="文件类型" width="200" align="left" prop="file_type" />
      <el-table-column label="文件名称" width="200" align="left" prop="filename"></el-table-column>
      <el-table-column label="文件大小" width="200" align="left" prop="file_size" />
      <el-table-column label="备注"  width="300" align="left" prop="filenotes"></el-table-column>
      <el-table-column label="新增时间"  width="200" align="leftr" prop="create_datetime"></el-table-column>
      <el-table-column fixed="right" width="155" label="下载操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['basic:categories:remove']"
            size="mini"
            type="text"
            @click="download(scope.row)"
          >下载</el-button>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="155" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['basic:categories:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类类型" prop="category_type">
          <el-select v-model="form.category_type" placeholder="请选择分类类型" clearable size="small">
            <el-option
              v-for="dict in categoryTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="软件许可协议文本" prop="eula_text">
          <el-input v-model="form.eula_text" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="默认的许可协议" prop="use_default_eula">
          <el-switch
            v-model="form.use_default_eula"
            active-color="#13ce66"
            inactive-color="#f1f1f1"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="同意许可协议" prop="require_acceptance">
          <el-switch
            v-model="form.require_acceptance"
            active-color="#13ce66"
            inactive-color="#f1f1f1"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="需要邮件通知" prop="checkin_email">
          <el-switch
            v-model="form.checkin_email"
            active-color="#13ce66"
            inactive-color="#f1f1f1"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="图片上传">
          <el-button
            type="primary"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="imageUpload"
          >图片上传
          </el-button>
          <img v-if="form.image" :src="form.image" class="avatar">
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 文件上传 -->
    <el-dialog :title="title1" :visible.sync="open1" width="400px" append-to-body @close="open1 = false">
      <FileUpload ref="saveFile" :file-type="['ALL']" @input="fileGet" />
      <el-form>
        <el-form-item label="备注" prop="eula_text">
          <el-input v-model="form.filenotes" type="textarea" placeholder="请输入备注内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="over()">完 成</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FileUpload from "@/components/FileUpload/indexPro";
import { addUploads, delUploads, listUploads } from "@/api/file";

export default {
  name: "Categories",
  components: { FileUpload },
  props: {
    categoryType: {
      type: String,
      default: "asset"
    },
    fileId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      categoryTypeOptions: [],
      imageUrl: "",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产分类表格数据
      categoriesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      title1: "",
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {
        filename: null,
        url: null,
        file_type: "",
        file_size: "",
        bind_id: "1",
        category_type: "",
        filenotes: ""
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        category_type: [
          { required: true, message: "分类类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getDicts("categories").then(response => {
      this.categoryTypeOptions = response.data;
      this.getList();
    });
  },
  methods: {
    download(row) {
      window.open(row.url);
    },
    over() {
      this.form.category_type = this.categoryType;
      if (!this.form.url) {
        this.$message({
          message: "请先上传文件",
          type: "error"
        });
        return;
      }
      this.form.bind_id = this.fileId;
      addUploads(this.form).then(response => {
        this.$message({
          message: "上传成功",
          type: "success"
        });
        this.open1 = false;
        this.getList();
        this.form.url = "";
        this.form.filenotes = "";
        this.$refs.saveFile.fileList = [];
      });
    },
    fileGet(data) {
      this.form.url = data.file;
      this.form.file_size = (data.size / 1024).toFixed(2) + "kb";
      this.form.file_type = data.type;
      this.form.filename = data.name;
    },
    imageUpload() {
      this.open1 = true;
      this.title1 = "文件上传";
    },
    /** 查询资产分类列表 */
    getList() {
      this.loading = true;
      this.queryParams.bind_id = this.fileId;
      this.queryParams.category_type = this.categoryType;
      listUploads(this.queryParams).then(response => {
        this.loading = false;
        this.categoriesList = response.data.results;
        // this.categoriesList.filter(res => {
        //   res.category_type = this.categoryTypeOptions.filter(item => item.dictValue === res.category_type)[0].dictLabel;
        // });
        this.total = response.data.count;
        this.$emit("updateNumber", this.total);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        description: null,
        modifier: null,
        dept_belong_id: null,
        update_datetime: null,
        create_datetime: null,
        name: null,
        category_type: null,
        deleted_datetime: null,
        eula_text: null,
        use_default_eula: null,
        require_acceptance: null,
        checkin_email: null,
        image: null,
        creator_id: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open1 = true;
      this.title1 = "文件上传";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
    },
    /** 提交按钮 */
    submitForm() {
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delUploads(ids).then(res => {
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
    }
  }
};
</script>
<style>
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  margin-top: 15px;
}
</style>
